
var Discussions_Impl = require('.././core/Discussions_Impl');

var gs3 = require('gs3');
var Entity = gs3.Models.Entity;

exports.test = function(req, res) {
    Discussions_Impl.post({name: 'this is test object'}, function(err, data) {
        res.send(data);
    });
};

exports.postComments = function(req, res) {
    var postId = req.params.postId;
    
    Discussions_Impl.getPostComments(postId, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.postsWithComments = function(req, res) {
    Discussions_Impl.getAllPostWithComments('5', ' ORDER BY id DESC', function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            var output = "";
            for (var key=0;key<data.length;key++) {
                announcementID = data[key].id+"";
                announcement = data[key].announcement;
                companyID = data[key].company_id+"";
                type = data[key].type+"";
                dateTime = data[key].date;
                userID = data[key].user_id+"";
                output += getannouncement(announcementID,announcement,companyID,type,dateTime,userID);
            }
            res.send(data);
        }
    });
};

exports.getPost = function(req, res) {
    var postId = req.params.postId;
    
    Discussions_Impl.getPost(postId, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};
function getannouncement(announcementID,announcement,companyID,type,dateTime,userID){
    var ret = '<li class="postContainerList" id="myPost_'+announcementID+'">';
	    ret +=  '<div>';
		ret +=  '<img src="images/avatar/avatar.png" width="60" height="55"/>';
	    ret +=  '</div>';
	    ret +=  '<div>';
		ret +=  '<p>';
		    ret +=  '<label class="postlblN"> '+userID+' </label>';
			ret += 	'<br>';
			ret +=  '<label class="postlblC">';
			    ret +=  announcement;
			ret +=  '</label>';
			ret +=  '<br>';
			ret +=  '<label class="postlblA">';
				ret +=  '<icon class="icon-time"></icon>&nbsp;';
				ret +=	'<label class="timeago" title="'+dateTime+'"> </label>';
			ret +=  '</label>';
			ret +=  '<label class="postlblActions showReply" data-announcement-id="'+announcementID+'"  data-action-post-type="rTP"> Reply  </label>';
			ret +=  '<label class="pull-right postlblActions deletePost" data-action="deletePost"  data-announcement-id="'+announcementID+'"> Delete  </label>';
			ret +=  '<div class="hr"></div>';
		ret +=  '</p>';
		// Append new comment created
		ret += 	'<div class="appendNewComment" id="appendNewComment_'+announcementID+'_rTP"></div>';
		//Comment view
		/*-----------------------------*/
		// Comment Reply
		ret +=  '<div class="commentAction display" id="showReply_'+announcementID+'_rTP">';
			ret +=  '<div>';
				ret +=  '<img src="images/avatar/avatar.png" width="35" height="35" style="border:2px solid #ddd;"/>';
			ret +=  '</div>';
			ret +=  '<div>';
				ret +=  '<textarea class="input-medium commentBox" id="newComment_'+announcementID+'_'+'rTP"></textarea>';
				ret +=  '<input type="button" class="button-icon commentbtn addButtonComment" data-action-post-type="rTP" data-announcement-id="'+announcementID+'" value="Send" />';
			ret +=  '</div>';
		ret +=  '</div>';		
	ret +=  '</div>';
ret += '</li>';
    
    return ret;
}
exports.newPost = function(req, res) {
//    var announcement = new Entity('announcement', 'id');
    var announcement = Discussions_Impl.getTopicEntity();

    announcement.fields.Message = req.body.Message || req.body.message;
//    announcement.User_id = req.session.user.id;
    announcement.fields.User_id = '1'; //  replace later after applying authentication

    Discussions_Impl.post(announcement, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.updatePost = function(req, res) {
//    var announcement = new Entity('announcement', 'id');
    var announcement = Discussions_Impl.getTopicEntity();
    
    announcement.fields.id = req.body.id || req.body.announcementId;
    announcement.fields.Message = req.body.Message || req.body.message;
//    announcement.User_id = req.session.user.id;
    announcement.fields.User_id = '1';
    
    Discussions_Impl.updatePost(announcement, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.deletePost = function(req, res) {
    var postId = req.params.postId;
//    var userId = req.session.user.id;
    var userId = '1';   //  replace later after applying authentication

    Discussions_Impl.deletePost(postId, userId, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.getComment = function(req, res) {
    var commentId = req.params.commentId;
    
    Discussions_Impl.getComment(commentId, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.comment = function(req, res) {
    var comment = Discussions_Impl.getCommentEntity();
//    var comment = new Entity('comment', 'id');    
    
    comment.fields.Message = req.body.Message || req.body.message;
//    comment.User_id = req.session.user.id;
    comment.fields.User_id = '1';  //  replace later after applying authentication
    comment.fields.Announcement_id = req.body.AnnouncementId || req.body.announcementid;
    
    Discussions_Impl.comment(comment, function(err, data) {
         if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};

exports.deleteComment = function(req, res) {
    var commentId = req.params.commentId;
//    var userId = req.session.user.id;
    var userId = '1';   //  replace later after applying authentication

    Discussions_Impl.deleteComment(commentId, userId, function(err, data) {
        if (err) {
            res.send(err.message);
        } else {
            res.send(data);
        }
    });
};